using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using OR.Lazarus.Business.Entities.ConfigList;

using OR.Lazarus.Config;

namespace OR.Lazarus.Presentation
{
	/// <summary>
	/// Summary description for LazarusSettings.
	/// </summary>
	public class LazarusSettings : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtConfigDir;
		private System.Windows.Forms.TextBox txtTransformDir;
		private System.Windows.Forms.TextBox txtSourceDir;
		private System.Windows.Forms.TextBox txtOutputDir;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnApply;
		private System.Windows.Forms.Button btnBrowseOutput;
		private System.Windows.Forms.TabControl tctTabs;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
        private System.Windows.Forms.TabPage tabGeneral;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public LazarusSettings()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.OnLoad();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txtConfigDir = new System.Windows.Forms.TextBox();
            this.txtTransformDir = new System.Windows.Forms.TextBox();
            this.txtSourceDir = new System.Windows.Forms.TextBox();
            this.txtOutputDir = new System.Windows.Forms.TextBox();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnApply = new System.Windows.Forms.Button();
            this.btnBrowseOutput = new System.Windows.Forms.Button();
            this.tctTabs = new System.Windows.Forms.TabControl();
            this.tabGeneral = new System.Windows.Forms.TabPage();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.tctTabs.SuspendLayout();
            this.tabGeneral.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label1.Location = new System.Drawing.Point(8, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 19);
            this.label1.TabIndex = 0;
            this.label1.Text = "Config";
            // 
            // label2
            // 
            this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label2.Location = new System.Drawing.Point(8, 48);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(56, 19);
            this.label2.TabIndex = 1;
            this.label2.Text = "Transforms";
            // 
            // label3
            // 
            this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label3.Location = new System.Drawing.Point(8, 80);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(56, 19);
            this.label3.TabIndex = 2;
            this.label3.Text = "Source";
            // 
            // label4
            // 
            this.label4.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label4.Location = new System.Drawing.Point(8, 112);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(56, 19);
            this.label4.TabIndex = 3;
            this.label4.Text = "Output";
            // 
            // txtConfigDir
            // 
            this.txtConfigDir.Location = new System.Drawing.Point(70, 16);
            this.txtConfigDir.Name = "txtConfigDir";
            this.txtConfigDir.Size = new System.Drawing.Size(394, 20);
            this.txtConfigDir.TabIndex = 0;
            // 
            // txtTransformDir
            // 
            this.txtTransformDir.Location = new System.Drawing.Point(70, 48);
            this.txtTransformDir.Name = "txtTransformDir";
            this.txtTransformDir.Size = new System.Drawing.Size(394, 20);
            this.txtTransformDir.TabIndex = 2;
            // 
            // txtSourceDir
            // 
            this.txtSourceDir.Location = new System.Drawing.Point(70, 80);
            this.txtSourceDir.Name = "txtSourceDir";
            this.txtSourceDir.Size = new System.Drawing.Size(394, 20);
            this.txtSourceDir.TabIndex = 4;
            // 
            // txtOutputDir
            // 
            this.txtOutputDir.Location = new System.Drawing.Point(70, 112);
            this.txtOutputDir.Name = "txtOutputDir";
            this.txtOutputDir.Size = new System.Drawing.Size(394, 20);
            this.txtOutputDir.TabIndex = 6;
            // 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnOK.Location = new System.Drawing.Point(328, 206);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(72, 23);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "OK";
            this.btnOK.Click += new System.EventHandler(this.DoOK);
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnCancel.Location = new System.Drawing.Point(408, 206);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(72, 23);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnApply
            // 
            this.btnApply.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnApply.Location = new System.Drawing.Point(488, 206);
            this.btnApply.Name = "btnApply";
            this.btnApply.Size = new System.Drawing.Size(72, 23);
            this.btnApply.TabIndex = 10;
            this.btnApply.Text = "Apply";
            this.btnApply.Click += new System.EventHandler(this.DoApply);
            // 
            // btnBrowseOutput
            // 
            this.btnBrowseOutput.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnBrowseOutput.Location = new System.Drawing.Point(472, 112);
            this.btnBrowseOutput.Name = "btnBrowseOutput";
            this.btnBrowseOutput.Size = new System.Drawing.Size(64, 20);
            this.btnBrowseOutput.TabIndex = 7;
            this.btnBrowseOutput.Text = "Browse...";
            this.btnBrowseOutput.Click += new System.EventHandler(this.btnBrowseOutput_Click);
            // 
            // tctTabs
            // 
            this.tctTabs.Controls.Add(this.tabGeneral);
            this.tctTabs.Location = new System.Drawing.Point(8, 8);
            this.tctTabs.Name = "tctTabs";
            this.tctTabs.SelectedIndex = 0;
            this.tctTabs.Size = new System.Drawing.Size(552, 192);
            this.tctTabs.TabIndex = 0;
            // 
            // tabGeneral
            // 
            this.tabGeneral.Controls.Add(this.button3);
            this.tabGeneral.Controls.Add(this.button2);
            this.tabGeneral.Controls.Add(this.button1);
            this.tabGeneral.Controls.Add(this.label1);
            this.tabGeneral.Controls.Add(this.label2);
            this.tabGeneral.Controls.Add(this.label3);
            this.tabGeneral.Controls.Add(this.label4);
            this.tabGeneral.Controls.Add(this.txtConfigDir);
            this.tabGeneral.Controls.Add(this.txtTransformDir);
            this.tabGeneral.Controls.Add(this.txtSourceDir);
            this.tabGeneral.Controls.Add(this.txtOutputDir);
            this.tabGeneral.Controls.Add(this.btnBrowseOutput);
            this.tabGeneral.Location = new System.Drawing.Point(4, 22);
            this.tabGeneral.Name = "tabGeneral";
            this.tabGeneral.Size = new System.Drawing.Size(544, 166);
            this.tabGeneral.TabIndex = 0;
            this.tabGeneral.Text = "General";
            // 
            // button3
            // 
            this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.button3.Location = new System.Drawing.Point(472, 16);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(64, 20);
            this.button3.TabIndex = 1;
            this.button3.Text = "Browse...";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button2
            // 
            this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.button2.Location = new System.Drawing.Point(472, 48);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(64, 20);
            this.button2.TabIndex = 3;
            this.button2.Text = "Browse...";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.button1.Location = new System.Drawing.Point(472, 80);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(64, 20);
            this.button1.TabIndex = 5;
            this.button1.Text = "Browse...";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // LazarusSettings
            // 
            this.ClientSize = new System.Drawing.Size(570, 234);
            this.Controls.Add(this.tctTabs);
            this.Controls.Add(this.btnApply);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOK);
            this.Font = new System.Drawing.Font("Tahoma", 8F);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "LazarusSettings";
            this.Text = "Lazarus Path Settings";
            this.Load += new System.EventHandler(this.LazarusSettings_Load);
            this.tctTabs.ResumeLayout(false);
            this.tabGeneral.ResumeLayout(false);
            this.tabGeneral.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		protected bool GetFolder( out string szFolder )
		{
            FolderBrowserDialog vFolderDialog = new FolderBrowserDialog();

			bool fRtnVal = false;

			szFolder = "";

            if (vFolderDialog.ShowDialog(this) == DialogResult.OK)
			{
                szFolder = vFolderDialog.SelectedPath;
				fRtnVal	= true;
			}

			return ( fRtnVal );
		}

		protected virtual void OnLoad()
		{
			txtConfigDir.Text		= LazarusConfig.ConfigPath;
			txtOutputDir.Text		= LazarusConfig.OutputPath;
			txtSourceDir.Text		= LazarusConfig.SourcePath;
			txtTransformDir.Text	= LazarusConfig.TransformPath;
		}

		protected virtual void OnSave()
		{
			LazarusConfig.ConfigPath		= txtConfigDir.Text;
			LazarusConfig.OutputPath		= txtOutputDir.Text;
			LazarusConfig.SourcePath		= txtSourceDir.Text;
			LazarusConfig.TransformPath	    = txtTransformDir.Text;

			LazarusConfig.Save();
		}

		private void DoApply(object sender, System.EventArgs e)
		{
			this.OnSave();
		}

		private void DoOK(object sender, System.EventArgs e)
		{
			this.OnSave();
		}

		private void btnBrowseOutput_Click(object sender, System.EventArgs e)
		{
			string szFolder;

			if ( GetFolder( out szFolder ) )
            txtOutputDir.Text = szFolder;
		}

		private void LazarusSettings_Load(object sender, System.EventArgs e)
		{
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			string szFolder;

			if ( GetFolder( out szFolder ) )
				txtConfigDir.Text = szFolder;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			string szFolder;

			if ( GetFolder( out szFolder ) )
				txtTransformDir.Text = szFolder;
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string szFolder;

			if ( GetFolder( out szFolder ) )
				txtSourceDir.Text = szFolder;
		}

		private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
		{
		
		}

		private void btnCancel_Click( object sender, EventArgs e )
		{

		}

		private void btnBrowseConfigList_Click( object sender, EventArgs e )
		{
			OpenFileDialog vDialog = new OpenFileDialog();

			vDialog.InitialDirectory = txtConfigDir.Text;

			vDialog.Filter = "XML File (*.xml)|*.xml";

			if ( vDialog.ShowDialog() == DialogResult.OK )
			{
				ConfigListDetail vList = new ConfigListDetail();

				try
				{
					vList.LoadXml( vDialog.FileName );
				}
				catch ( Exception ex )
				{
					throw new ApplicationException( string.Format( "Loading of ConfigList file [ {0} ] failed: {1}", vDialog.FileName, ex.Message ), ex );
				}
			}
		}
	}
}
